// TODO: integrate measures (meters) into Entity class

function Entity(){}

Entity.prototype.x = 0;
Entity.prototype.y = 0;
Entity.prototype.z = 0;
Entity.prototype.filters = [];
Entity.prototype.draw = function(graphics){}
Entity.prototype.update = function(){}

Entity.extend("EntityGroup");
EntityGroup.prototype.entities = [];
EntityGroup.prototype.addEntity = function(entity){
    this.entities.push(entity);
}
EntityGroup.prototype.removeEntity = function(entity){
    this.entities.remove(entity);
}
EntityGroup.prototype.draw = function(graphics){
    this.entities.sort(function(a, b){
        return b.z - a.z;
    });
	var i = this.entities.length;
	while(i--){
		var ent = this.entities[i];
        var origX = ent.x;
        var origY = ent.y;
        var origZ = ent.z;
        ent.x += this.x;
        ent.y += this.y;
        ent.z += this.z;
        ent.draw(graphics);
        ent.x = origX;
        ent.y = origY;
        ent.z = origZ;
	}
}

EntityGroup.prototype.update = function(context){
    this.entities.sort(function(a, b){
        return b.z - a.z;
    });
	var i = this.entities.length;
	while(i--){
		this.entities[i].update();
	}
}

Entity.extend("Rectangle");
Rectangle.prototype.__constructor = function(width, height){
    this.width = width;
    this.height = height;
}
Rectangle.prototype.width = 0;
Rectangle.prototype.height = 0;
Rectangle.prototype.fillColor = "#000";
Rectangle.prototype.borderColor = "#000";
Rectangle.prototype.borderVisible = false;
Rectangle.prototype.fillVisible = true;

Rectangle.prototype.setWidth = function(width){
    this.width = width;
}

Rectangle.prototype.setHeight = function(height){
    this.height = height;
}

Rectangle.prototype.setFillColor = function(color){
    this.fillColor = color;
}

Rectangle.prototype.setBorderColor = function(color){
    this.borderColor = color;
}

Rectangle.prototype.setBorderVisible = function(value){
    this.borderVisible = value;
}

Rectangle.prototype.setFillVisible = function(value){
    this.fillVisible = value;
}

Rectangle.prototype.draw = function(graphics){
    if(this.fillVisible === true){
        graphics.setFillStyle(this.fillColor);
        graphics.fillRect(this.x, this.y, this.width, this.height);
    }
    if(this.borderVisible === true){
        graphics.setStrokeStyle(this.borderColor);
        graphics.strokeRect(this.x, this.y, this.width, this.height);
    }
}

Entity.extend("Circle");
Circle.prototype.__constructor = function(radius){
    this.radius = radius;
}
Circle.prototype.radius = 0;
Circle.prototype.fillColor = "#000";
Circle.prototype.borderColor = "#000";
Circle.prototype.borderVisible = false;
Circle.prototype.fillVisible = true;

Circle.prototype.setRadius = function(radius){
    this.radius = radius;
}

Circle.prototype.setFillColor = function(color){
    this.fillColor = color;
}

Circle.prototype.setBorderColor = function(color){
    this.borderColor = color;
}

Circle.prototype.setBorderVisible = function(value){
    this.borderVisible = value;
}

Circle.prototype.setFillVisible = function(value){
    this.fillVisible = value;
}

Circle.prototype.draw = function(graphics){
    if(this.fillVisible === true){
        graphics.setFillStyle(this.fillColor);
        graphics.fillCircle(this.x, this.y, this.radius);
    }
    if(this.borderVisible === true){
        graphics.setStrokeStyle(this.borderColor);
        graphics.strokeCircle(this.x, this.y, this.radius);
    }
}